<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller\ding;

use app\api\controller\Controller;
use app\api\model\ding\Coupon as CouponModel;
use think\facade\Db;
use think\response\Json;

/**
 * 余额账单明细
 * Class Log
 * @package app\api\controller\balance
 */
class Coupon extends Controller
{
    /**
     * 余额账单明细列表
     * @return Json
     * @throws \cores\exception\BaseException
     * @throws \think\db\exception\DbException
     */
    public function list(): Json
    {
        $model = new CouponModel;
        $list  = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }
    public function add(): Json
    {
        $model = new CouponModel;
        if (! $model->add($this->postForm())) {
            return $this->renderError('新增失败');
        }

        return $this->renderSuccess([], '新增成功');
    }
     public function edit($couponId): Json
    {
       $model  = new CouponModel;
        $detail = $model->detail($couponId);
        if (! $detail->edit($this->postForm())) {
            return $this->renderError('编辑失败');
        }

        return $this->renderSuccess([], '编辑成功');
    }
    public function typeList(): Json
    {

        $list = Db::name('ding_coupon_category')->select()->toArray();
        return $this->renderSuccess(compact('list'));
    }
    public function detail($couponId): Json
    {

        $model  = new CouponModel;
        $detail = $model->detail($couponId);

        $detail['coupon_type_text'] = Db::name('ding_coupon_category')->where('category_id', $detail['coupon_type'])
            ->value('name');
        return $this->renderSuccess(compact('detail'));
    }
}
